using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using DTO;
using BUS;
using Utility;

public partial class Doanhnghiep_ChiTietSanPham_DN : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"]);
            tbSanPham sp = tbSanPhamBUS.GetByID(id);
            lit_masp.Text = sp.masp.ToString();


            hpl_them.NavigateUrl = "~/DoanhNghiep/HinhAnh_Add.aspx?id=" + sp.masp.ToString();

            lit_tensp.Text = sp.tensp;

            tbDanhMucSanPham dm = tbDanhMucSanPhamBUS.GetByID(sp.danhmuc);
            lit_danhmuc.Text = dm.tendanhmuc;

            tbDoanhNghiep dn = tbDoanhNghiepBUS.GetByID(sp.doanhnghiep);
            lit_doanhnghiep.Text = dn.tendn;

            Literal1.Text = sp.tinvantat.ToString();
            lit_giagoc.Text = sp.giagoc.ToString();
            lit_giaban.Text = sp.giaban.ToString();
            lit_soluongban.Text = sp.soluongban.ToString();
            lit_soluongtoida.Text = sp.soluongtoida.ToString();
            lit_songuoidamua.Text = sp.damua.ToString();
            lit_chietkhau.Text = sp.chietkhau.ToString();
            lit_ngaysudung.Text = sp.ngaysudung.ToString();
            lit_ngayhethan.Text = sp.ngayhethan.ToString();
            lit_mota.Text = sp.mota;

            tbTinhTrang tt = tbTinhTrangBUS.GetByID(sp.tinhtrang);
            lit_tinhtrang.Text = tt.tentinhtrang;

            lit_quydinh.Text = sp.quydinh;


            img_HinhMinhHoa.ImageUrl =
                "~/Photos/Thumb/" + sp.hinhchinh;

            Pager.PageSize = 5;
            Criteria c = new Criteria();
            c.add(Condition.AND, tbSanPhamColumns.masp, Condition.EQUAL, id);
            int total = 0;
            tbHinhAnhCollection haCollection =
                tbHinhAnhBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
                tbHinhAnhColumns.masp.ToString(), OrderDirection.ASC.ToString(), out total);
            Repeater1.DataSource = haCollection;
            Repeater1.DataBind();
            Pager.ItemCount = total;
        }
        catch
        {
        }

    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
         e.Item.ItemType == ListItemType.Item)
        {
            tbHinhAnh ha = (tbHinhAnh)e.Item.DataItem;
            Image img_hinhanh = (Image)e.Item.FindControl("img_hinhanh");
            img_hinhanh.ImageUrl = "~/Photos/Thumb/" + ha.hinhminhhoa;

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/DoanhNghiep/UpdateHinh.aspx?id=" + ha.maha.ToString();
        }
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbHinhAnh ha = tbHinhAnhBUS.GetByID(id);
                if (ha.hinhminhhoa != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + ha.hinhminhhoa));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + ha.hinhminhhoa));
                }
                tbHinhAnhBUS.Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}
